﻿using ORMDemo.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ORMDemo.Dal
{
    internal class SQLConnectionStringPool
    {
        private static long _readIndex = 0;

        private static string ConnectionStringWrite = ConfigurationManager.SqlConnectionStringWrite;//读取sql写入数据库字符串
        private static string[] ConnectionStringRead = ConfigurationManager.SqlConnectionStringRead;//读取sql写入数据库字符串
        internal static string GetConnectionString(DBOperateType dBOperateType)
        {
            string connection = string.Empty;

            switch (dBOperateType)
            {
                case DBOperateType.Write:
                    connection = ConnectionStringWrite; break;
                case DBOperateType.Read:
                    connection = DispatcherConn();
                    break;
            }
            return connection;
        }
        private string[] connAll = new string[] {
             ConnectionStringRead[0],
             ConnectionStringRead[1],
             ConnectionStringRead[1],
             ConnectionStringRead[1],
             ConnectionStringRead[2],
             ConnectionStringRead[2],
             ConnectionStringRead[2],
             ConnectionStringRead[2],
             ConnectionStringRead[2],
             ConnectionStringRead[2]
            };
        private static string DispatcherConn()
        {
            //return ConnectionStringRead[_readIndex++ % ConnectionStringRead.Length];  //轮询-平均
            return ConnectionStringRead[new Random().Next(0, ConnectionStringRead.Length)];  //随机-平均
            //随机-权重 --首先在app中配置---读取  这里先写死 1，3，6
            //return connAll[new Random().Next(0, ConnectionStringRead.Length) ];  
        }
        //能不能根据数据库的压力来分配？
        //一定得有个实时获取压力的途径
        //a可以根据查询的响应时间
        //b需要途径去获取硬件信息
        //c实时获取数据库连接数
        internal enum DBOperateType
        {
            Write,
            Read
        }
    }
}
